#!/usr/bin/perl
#
# Delete a place record
#
use Counter;
use POSIX;
use ErrorForm;
use Counter::CGI;

$] > 5.008 && binmode(STDOUT, ":utf8"); # set web page output to utf-8

$q = new Counter::CGI;
$key = $q->param("key");
errorForm "Place not specified" if !$key;
$file = FileTable::open("db/places", O_RDWR);
$pers = FileTable::open("db/persons"); # for checking persons
$rec = $file->get($key);
if (!$rec) {
    errorForm "no such place as $key\n";
}
$recname = $rec->{name};
#$within = $file->get($rec->{within}) if $rec->{within};
#
# delete the record. Verification is for chickens!
#
@personshere = $pers->select("placeid = ?", $rec->{name});
@placeshere = $file->select("within = ?", $rec->key);

if ($#placeshere > -1) {
    errorForm "Cannot delete - contains other places";
}

$rec->delete();
print <<EoF;
Content-type: text/html; charset="utf-8"

<html><head>
    <title>Linux Counter Place Deleted</title>
</head><body bgcolor=white>
It used to be $recname.<br>
It's gone.
EoF
if ($#personshere > -1) {
   print "There used to be ", $#personshere+1, " people listed here.<br>\n";
   print "You'd better get their records recleaned.\n";
}

print <<EoF;
</body>
EoF
